#include <ToolManager>
#include <Tool>
#include <ToolCategory>

ToolManager::ToolManager(QObject *parent)
    : QObject(parent)
{

}

bool ToolManager::registerCategory(QPointer<ToolCategory> category)
{
    if(m_categories.contains(category)) return false;
    m_categories.append(category);
    emit toolCategoryAdded(category);

    return true;
}

bool ToolManager::registerTool(QPointer<Tool> tool)
{
    if(QPointer<ToolCategory> cat = categoryByName(tool->categoryName()))
    {
        tool->setIcon(QIcon(tool->iconPath()));
        tool->initPropertiesWidget();

        bool bResult = cat->addTool(tool);

        if(bResult)
        {
            emit toolAdded(tool);
        }
    }

    return false;
}

QPointer<ToolCategory> ToolManager::categoryByName(const QString& catName) const
{
    QPointer<ToolCategory> result;

    foreach(QPointer<ToolCategory> c, m_categories)
    {
        if(c->name() == catName)
        {
            result = c;
            break;
        }
    }

    return result;
}

